import java.util.Map;
import java.util.Scanner;

/**
 * 控制台商品管理系统，处理用户交互
 */
public class ConsoleShop {
    private static ProductService productService = new ProductService();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("===== 商品管理系统 =====");
        while (true) {
            printMenu();
            int choice = scanner.nextInt();
            scanner.nextLine();  // 吸收换行符
            handleChoice(choice);
        }
    }

    /**
     * 打印操作菜单
     */
    private static void printMenu() {
        System.out.println("\n请选择操作：");
        System.out.println("1. 添加商品");
        System.out.println("2. 查看所有商品");
        System.out.println("3. 查询商品");
        System.out.println("0. 退出系统");
        System.out.print("请输入选项：");
    }

    /**
     * 处理用户选择
     */
    private static void handleChoice(int choice) {
        switch (choice) {
            case 1:
                addProduct();
                break;
            case 2:
                showAllProducts();
                break;
            case 3:
                queryProduct();
                break;
            case 99:  // 隐藏的管理员备份选项
                productService.backupData();
                break;
            case 0:
                exitSystem();
                break;
            default:
                System.out.println("无效选项，请重新输入");
        }
    }

    /**
     * 添加商品
     */
    private static void addProduct() {
        System.out.println("\n----- 添加商品 -----");
        System.out.print("请输入商品ID：");
        String id = scanner.nextLine();
        System.out.print("请输入商品名称：");
        String name = scanner.nextLine();
        System.out.print("请输入商品价格：");
        double price = scanner.nextDouble();
        System.out.print("请输入商品库存：");
        int stock = scanner.nextInt();
        scanner.nextLine();  // 吸收换行符

        productService.addProduct(new Product(id, name, price, stock));
        System.out.println("商品添加成功");
    }

    /**
     * 查看所有商品
     */
    private static void showAllProducts() {
        System.out.println("\n----- 所有商品 -----");
        Map<String, Product> products = productService.getAllProducts();
        if (products.isEmpty()) {
            System.out.println("暂无商品数据");
            return;
        }
        for (Product p : products.values()) {
            System.out.println(p);
        }
    }

    /**
     * 查询商品
     */
    private static void queryProduct() {
        System.out.print("\n请输入要查询的商品ID：");
        String id = scanner.nextLine();
        Product product = productService.getProduct(id);
        if (product != null) {
            System.out.println("查询结果：" + product);
        } else {
            System.out.println("未找到ID为" + id + "的商品");
        }
    }

    /**
     * 退出系统（保存数据）
     */
    private static void exitSystem() {
        productService.saveData();  // 退出前保存数据
        System.out.println("系统已退出，感谢使用");
        scanner.close();
        System.exit(0);
    }
}